package com.yuncool.sky.system.client;

import com.yuncool.sky.system.domain.SelectRecord;
import com.yuncool.sky.system.domain.Subject;
import com.yuncool.sky.system.pojo.bo.SubjectBO;
import com.yuncool.sky.system.pojo.form.SubjectForm;
import com.yuncool.sky.system.service.SelectRecordService;
import com.yuncool.sky.system.service.SubjectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Auther XIE
 * @Date 2019/11/24 17:28
 * @Description RabbitMQ消费者
 * @Version 1.0
 */
@Component
@Slf4j
public class RabbitClient {
    @Autowired
    SelectRecordService recordService;
    @Autowired
    SubjectService subjectService;

    /**
     * 消费-抢课
     *
     * @param subjectBO
     */
    @RabbitListener(queues = "select-queue")
    public void sendSelectSubject(SubjectBO subjectBO) {
        log.info("减少....");
        Subject subject = new Subject();
        subject.setId(subjectBO.getId());
        subject.setSold(subjectBO.getSold());
        subjectService.updateById(subject);
    }

    /**
     * 消费-记录
     *
     * @param subjectForm
     */
    @RabbitListener(queues = "record-queue")
    public void sendSelectSubject(SubjectForm subjectForm) {
        log.info("记录....");
        SelectRecord record = new SelectRecord();
        record.setSubjectId(Integer.valueOf(subjectForm.getSid()));
        record.setUserNumber(subjectForm.getUserNumber());
        record.setCreateTime(new Date());
        recordService.save(record);
    }
}
